
import os, time

from matplotlib import pyplot

from dexen.libs import constants as dc, dexen_api
from scripts.utils import input_reading, constants
from scripts.utils.visualizer import RectangleVisualizer

""" GLOBALS """
script_api = dexen_api.getScriptApi()
boxes = input_reading.read_input()

def draw_pareto():
    #print "I am in draw pareto"
    
    individuals = script_api.downloadIndividuals(
        select = ("min_width", "min_height", 'orient', 'phenotype', 'area'),
        where = (
            ("state", dc.EQUAL, constants.ALIVE),
            ("area", dc.NOT_EQUAL, None)
        ),
        order_by = ("area", dc.ASC)
    ) 

    #print "The no of inds", len(individuals)
    
    #print "The global best area so far is", individuals[0]['area'], individuals[0]['min_width'], individuals[0]['min_height']
    pyplot.scatter([individual['min_width'] for individual in individuals],
                   [individual['min_height'] for individual in individuals]
    )
    pyplot.savefig(os.path.join(constants.VISUALIZATION_DIR, "pareto.png"))
    draw_rectangles(individuals)

def draw_rectangles(individuals):
    for id, individual in enumerate(individuals):
        rv = RectangleVisualizer(500, 500, boxes)
        rv.draw_individual(individual)
        rv.save_to_file(os.path.join(constants.VISUALIZATION_DIR, str(id) + ".png"))

